package a630wj.recycleview630wj.model;

import java.util.List;

import a630wj.recycleview630wj.bean.CourseBean;
import a630wj.recycleview630wj.iface.CourseListener;
import a630wj.recycleview630wj.iface.Courseiface;
import a630wj.recycleview630wj.service.CourseService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by weijie on 2017/6/9.
 */

public class CourseModel implements Courseiface {
    private Retrofit retrofit;
    private String BASEURL = "http://amicool.neusoft.edu.cn/";

    public CourseModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final CourseListener listener) {
        CourseService courseservice = retrofit.create(CourseService.class);
        Call<List<CourseBean>> call = courseservice.getCourseList(mod, page, sessionID);
        call.enqueue(new Callback<List<CourseBean>>() {
            @Override
            public void onResponse(Call<List<CourseBean>> call, Response<List<CourseBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("FALL");
                }
            }


            @Override
            public void onFailure(Call<List<CourseBean>> call, Throwable t) {
                listener.onFail("FAIL");
            }
        });
    }
}
